package com.shujia.wyh.day10;

import java.util.Iterator;
import java.util.LinkedList;

/*
    LinkedList：底层是双链表的数据结构，查询慢，增删快，线程不安全，效率高

    特有方法：
        public void addFirst(E e)及addLast(E e)
        public E getFirst()及getLast()
        public E removeFirst()及public E removeLast()

 */
public class LinkedListDemo1 {
    public static void main(String[] args) {
        //1、创建集合对象
        LinkedList linkedList = new LinkedList();

        //2、创建元素对象并添加到集合中
        linkedList.add("hello");
        linkedList.add("world");
        linkedList.add("java");
        linkedList.add("hello");
        linkedList.add("hadoop");
        System.out.println(linkedList);
        System.out.println("======================");

        //3、遍历集合
        //List集合有两种遍历方式：1、size()方法和迭代器
//        Iterator iterator = linkedList.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }

        //addFirst(E e)及addLast(E e)
        linkedList.addFirst("spark");
        linkedList.addLast("hive");  //效果和add()方法一样

        System.out.println(linkedList);


        //public E getFirst()及getLast()
        System.out.println(linkedList.getFirst());
        System.out.println(linkedList.getLast());
        System.out.println("=======================================");
        //public E removeFirst()及public E removeLast()
        System.out.println(linkedList.removeFirst());
        System.out.println(linkedList.removeLast());
        System.out.println(linkedList);
    }
}
